import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '3Dball/user_level_model.dart';
import '3Dball/xball_view.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(750, 1334),
    
      builder: () =>MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: '3D-ball'),
    ));
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
    late List<UserLevelModel> themeList;
  late List<UserLevelModel> coreList;
  late List<UserLevelModel> lineList;
  // late List<GradeModel> gradeList;
  Map students = {};
    @override
  void initState() {
    super.initState();
    getLocalData();
  }

    //获取本地json
  Future getLocalData() async {
    rootBundle.loadString("images/3d.json").then((value) {
      Map map = jsonDecode(value);

      if (map['motif'] != null) {
        List motif = map['motif'];
        List<UserLevelModel> motifDemo = [];
        for (var i = 0; i < motif.length; i++) {
          UserLevelModel model = UserLevelModel.fromJson(motif[i]);
          motifDemo.add(model);
        }
        setState(() {
          themeList = motifDemo;
        });
      }
      if (map['norm'] != null) {
        List norm = map['norm'];
        List<UserLevelModel> normDemo = [];
        for (var i = 0; i < norm.length; i++) {
          UserLevelModel model = UserLevelModel.fromJson(norm[i]);
          normDemo.add(model);
        }
        setState(() {
          coreList = normDemo;
        });
      }
      if (map['line'] != null) {
        List line = map['line'];
        List<UserLevelModel> lineDemo = [];
        for (var i = 0; i < line.length; i++) {
          UserLevelModel model = UserLevelModel.fromJson(line[i]);
          lineDemo.add(model);
        }
        setState(() {
          lineList = lineDemo;
        });
      }
    });
  }
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
  
        title: Text(widget.title),
      ),
      body: Center(
 
        child: Column(
       
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
           XBallView(
                    mediaQueryData: MediaQuery.of(context),
                    keywords: themeList,
                    highlight: coreList,
                    lineList: lineList, 
                  ),
          ],
        ),
      ),
     
    );
  }
}
